package com.ying.controller.order;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ying.core.page.PageWrap;
import com.ying.core.response.ApiResponse;
import com.ying.po.order.Orange;
import com.ying.service.order.OrangeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/api/order/orange")
@Api(tags = "Orange接口")
public class OrangeController {

    @Autowired
    private OrangeService orangeService;


    @GetMapping("/{id}")
    @ApiOperation("根据ID查询Orange")
    @SaCheckPermission("orange-get")
    public ApiResponse finById(@PathVariable(value = "id") Integer id) {
        return ApiResponse.success(orangeService.findById(id));
    }

    @PostMapping("/create")
    @ApiOperation("创建Orange对象")
    @SaCheckPermission("orange-post")
    public ApiResponse create(@RequestBody Orange orange) {
        // 存储Orange
        Integer id = orangeService.create(orange);
        return ApiResponse.success(id);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("根据ID删除")
    @SaCheckPermission("orange-delete")
    public ApiResponse delete(@PathVariable Integer id) {
        orangeService.deleteById(id);
        return ApiResponse.success(null);
    }

    @DeleteMapping("/delete")
    @ApiOperation("根据ID列表批量删除")
    @SaCheckPermission("orange-delete")
    public ApiResponse deleteByIds(@RequestBody List<Integer> ids) {
        orangeService.deleteByIdInBatch(ids);
        return ApiResponse.success(null);
    }

    @GetMapping("/countall")
    @ApiOperation("查询数量")
    @SaCheckPermission("orange-get")
    public ApiResponse countAll() {
        long count = orangeService.countAll();
        return ApiResponse.success(count);
    }

    @PostMapping("/findbyids")
    @ApiOperation("根据ID列表查询")
    @SaCheckPermission("orange-get")
    public ApiResponse findByIds(@RequestBody Integer[] ids) {
        List<Orange> userList = orangeService.findByIdInBatch(Arrays.asList(ids));
        return ApiResponse.success(userList);
    }

    @GetMapping("/find/{year}")
    @ApiOperation("查询某年全部")
    @SaCheckPermission("orange-get")
    public ApiResponse findYearAll(@PathVariable("year") String year, @RequestParam("userId") Integer userId) {
        return ApiResponse.success(orangeService.findYearAll(year, userId));
    }

    @GetMapping("/find/during")
    @SaCheckPermission("orange-get")
    @ApiOperation("查询某个时间段的orange对象")
    public ApiResponse findYearDuring(@RequestParam("year1") Integer year1, @RequestParam("year2") Integer year2, @RequestParam("userId") Integer userId) {
        return ApiResponse.success(orangeService.findYearDuring(year1, year2, userId));
    }

//    getOrangePrice
    @GetMapping("/price/all")
    @SaCheckPermission("orange-get")
    @ApiOperation("查询本年所有的orange价格")
    public ApiResponse getOrangePrice(@RequestParam("userId") Integer userId) {
        return ApiResponse.success(orangeService.getOrangePrice(userId));
    }

}
